package pack07_combiner_wordcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 1:自定义类，继承Reducer类
 *   四个泛型： K2，V2，K3，V3类型
 */
public class WordCountReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
    //2：重写reduce方法,该方法执行多少次数由不重复的单词个数来决定

    @Override              //K2            //[V2]
    protected void reduce(Text key, Iterable<LongWritable> values,Context context) throws IOException, InterruptedException {
        //3:在方法中将K2，[V2]转为K3和V3
        //3.1 得到K3，K2就是K3
        //3.2 得到V3，将[V2]中的数字进行累加，得到V3
        //3.3 将K3和V3写入下一个处理环节
        long count = 0;
        for (LongWritable value : values) {
            count += value.get(); //V3
        }

        context.write(key,new LongWritable(count));

    }
}
